<?php
/********************************\
 * 框架核心文件: BaseModel.php
 * 功能：数据库的相关操作
 * 模型要继承本类才能连接数据库
 +-------------------------------
 * www.cnblogs.com/SmilingLiuWei
 * SmilingLiuWei@163.com
\********************************/

class BaseModel {
	/**
	 **************** 添加数据 **************
	 +----------------------------------------
	 * 数据必须是一个数组：列1=>值1，列2=>值2
	 * $obj.add('table_name', $_POST);
	 +----------------------------------------
	 */
	public function add($table, &$data){
		$fields	= mysql_list_fields($this->config['database'], $table);
		$fields_num = mysql_num_fields($fields);
		$sql	= "insert into $table set";
		for($i=0; $i<$fields_num; $i++){
			$field_name = mysql_field_name($fields, $i);
			if(isset($data[$field_name]))
				$sql .= "`$field_name`='{$data[$field_name]}',";
		}
		$sql = rtrim($sql, ',');
		echo $sql;
		$bool = mysql_query($sql.";")
			or die("<br>There is a error in add()<br>$sql<br>".mysql_error()."<br>");

		return $bool;
	}

	/**
	 **************** 删除数据 ****************
	 +-----------------------------------------
	 * $obj.del('table_name', "id='1'");
	 +-----------------------------------------
	 */
	public function del($table, $where=false){
		$where	= $where ? $where : 1;
		$sql	= "delete from $table where $where;";
		return $this->query($sql);
	}

	/**
	 ************** 调用系统函数 **************
	 +-----------------------------------------
	 * $model->qurey($sql);
	 +-----------------------------------------
	 */
	public function query($sql) {
		return mysql_query();
	}

	/**
	 **************** 初始化 ****************
	 +-----------------------------------------
	 * $model = M();
	 +-----------------------------------------
	 */
	public function __construct(){
	}


	private $config;
}
